package com.znyj.servlet.dao;


import com.znyj.servlet.entity.UserEntity;
import com.znyj.servlet.sqlHelper.SqlProvider;
import com.znyj.servlet.sqlHelper.SqlRequest;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by gaoliwei02 on 17/7/6.
 */

@Repository
public interface UserDao {
    public static final String SELECT_KEYS = " " +
            "id," +
            "username," +
            "password," +
            "last_sign_time," +
            "register_time," +
            "sign_status," +
            "phone_number," +
            "role_permission," +
            "ip_address," +
            "note";
    public static final String INSERT_KEYS = " " +
            "username," +
            "password," +
            "last_sign_time," +
            "register_time," +
            "sign_status," +
            "phone_number," +
            "role_permission," +
            "ip_address," +
            "note";
    public static final String INSERT_VALUES = " " +
            "#{userName}," +
            "#{passWord}," +
            "#{lastSignTime}," +
            "#{registerTime}," +
            "#{signStatus}," +
            "#{phoneNumber}," +
            "#{rolePermission}," +
            "#{ipAddress}," +
            "#{note}";

    @Insert("insert into cdb_user (" + INSERT_KEYS + ") values (" + INSERT_VALUES + ") ")
    public int insert(UserEntity entity);

    @Update("update cdb_user set " +
            "password=#{passWord}," +
            "last_sign_time=#{lastSignTime}," +
            "register_time=#{registerTime}," +
            "sign_status=#{signStatus}," +
            "phone_number=#{phoneNumber}," +
            "role_permission=#{rolePermission}," +
            "ip_address=#{ipAddress}, " +
            "note=#{note} " +
            "where username=#{userName}")
    public int update(UserEntity entity);

    @Select(" select * from cdb_user where username=#{userName}")
    public UserEntity findByUsername(@Param("userName") String userName);

    @Update("update cdb_user set " +
            "last_sign_time=#{lastSignTime}," +
            "sign_status=#{signStatus}," +
            "ip_address=#{ipAddress} " +
            "where username=#{userName}")
    public int updateUserInfo(@Param("lastSignTime") Timestamp lastSignTime,
                              @Param("signStatus") int signStatus,
                              @Param("ipAddress") String ipAddress,
                              @Param("userName") String userName);

    @SelectProvider(type = SqlProvider.class, method = "query")
    public List<UserEntity> query(SqlRequest request);

    @SelectProvider(type = SqlProvider.class, method = "count")
    public int count(SqlRequest request);
}
